package com.example.utils.tool;

import java.security.SecureRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: TODO
 * @Author runto
 * @Date 2023/10/14 10:15
 * @PackageName:com.example.utils.tool
 * @ClassName: StringUtil
 * @Version 1.0
 */
public class StringUtil {

    public StringUtil() {}

    public static boolean isBlank(String str) {
        if (null == str) {
            return true;
        } else {
            return "".equals(str.trim());
        }
    }

    public static String toString(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static String restrictLength(String strSrc, int iMaxLength) {
        if (strSrc == null) {
            return null;
        } else if (iMaxLength <= 0) {
            return strSrc;
        } else {
            String strResult = strSrc;
            byte[] b = null;
            int iLength = strSrc.length();
            if (iLength > iMaxLength) {
                strResult = strSrc.substring(0, iMaxLength);
                iLength = iMaxLength;
            }

            while(true) {
                b = strResult.getBytes();
                if (b.length <= iMaxLength) {
                    return strResult;
                }

                --iLength;
                strResult = strResult.substring(0, iLength);
            }
        }
    }

    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        SecureRandom random = new SecureRandom();
        StringBuffer buf = new StringBuffer();

        for(int i = 0; i < length; ++i) {
            int num = random.nextInt(str.length());
            buf.append(str.charAt(num));
        }

        return buf.toString();
    }

    public static String lPad(String target, String fix, int length) {
        if (target != null && fix != null && target.length() < length) {
            StringBuffer newStr = new StringBuffer();

            for(int i = 0; i < length - target.length(); ++i) {
                newStr.append(fix);
            }

            return newStr.append(target).toString();
        } else {
            return target;
        }
    }

    public static String rPad(String target, String fix, int length) {
        if (target != null && fix != null && target.length() < length) {
            StringBuffer newStr = new StringBuffer();
            newStr.append(target);

            for(int i = 0; i < length - target.length(); ++i) {
                newStr.append(fix);
            }

            return newStr.toString();
        } else {
            return target;
        }
    }

    public static String join(String[] strs, String spi) {
        StringBuffer buf = new StringBuffer();
        int step = 0;
        String[] var4 = strs;
        int var5 = strs.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String str = var4[var6];
            buf.append(str);
            if (step++ < strs.length - 1) {
                buf.append(spi);
            }
        }

        return buf.toString();
    }

    public static String toString2(Object obj) {
        if (obj == null) {
            return "无";
        } else {
            return obj == "" ? "无" : obj.toString();
        }
    }

    public static String replaceServiceNumBar(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("-");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }

        return dest;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    public static String convertFieldToSetMethod(String value) {
        StringBuilder builder = new StringBuilder("set");
        String lowerValue = value.toLowerCase();
        String[] arrays = lowerValue.split("_");
        String[] var4 = arrays;
        int var5 = arrays.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String str = var4[var6];
            String upperStr = upperCaseFirstChar(str);
            builder.append(upperStr);
        }

        return builder.toString();
    }

    public static String upperCaseFirstChar(String value) {
        char[] cs = value.toCharArray();
        cs[0] = (char)(cs[0] - 32);
        return String.valueOf(cs);
    }

    public static String lowerCaseFirstChar(String value) {
        char[] cs = value.toCharArray();
        cs[0] = (char)(cs[0] + 32);
        return String.valueOf(cs);
    }
}

